import {
  chatHistoryValueDesc,
  FlowNodeInputTypeEnum,
  FlowNodeOutputTypeEnum,
  FlowNodeTemplateTypeEnum,
  FlowNodeTypeEnum,
  NodeInputKeyEnum,
  NodeOutputKeyEnum,
  WorkflowIOValueTypeEnum,
} from '@/types/workflow/constant';
import { FlowNodeTemplateType } from '@/types/workflow/node';
import { getHandleConfig } from '../../utils';
import { t } from '@/locales/i18n';
import {
  Input_Template_Dataset_Quote,
  Input_Template_History,
  Input_Template_SettingAiModel,
  Input_Template_System_Prompt,
  Input_Template_Text_Quote,
  Input_Template_UserChatInput,
  User_Input_Template_UserChatInput,
} from '../../input';
import { chatNodeSystemPromptTip, systemPromptTip } from '../../tip';

export const AiChatQuoteRole = {
  key: NodeInputKeyEnum.aiChatQuoteRole,
  renderTypeList: [FlowNodeInputTypeEnum.hidden],
  label: '',
  valueType: WorkflowIOValueTypeEnum.string,
  value: 'system', // user or system
};
export const AiChatQuoteTemplate = {
  key: NodeInputKeyEnum.aiChatQuoteTemplate,
  renderTypeList: [FlowNodeInputTypeEnum.hidden],
  label: '',
  valueType: WorkflowIOValueTypeEnum.string,
};
export const AiChatQuotePrompt = {
  key: NodeInputKeyEnum.aiChatQuotePrompt,
  renderTypeList: [FlowNodeInputTypeEnum.hidden],
  label: '',
  valueType: WorkflowIOValueTypeEnum.string,
};

export const AiChatModule: FlowNodeTemplateType = {
  id: FlowNodeTypeEnum.chatNode,
  templateType: FlowNodeTemplateTypeEnum.ai,
  flowNodeType: FlowNodeTypeEnum.chatNode,
  sourceHandle: getHandleConfig(true, true, true, true),
  targetHandle: getHandleConfig(true, true, true, true),
  avatar: 'aiChat.svg',
  name: t('template.ai_chat'),
  intro: t('workflow.model_chat_desc'),
  showStatus: true,
  isTool: true,
  courseUrl: '/docs/workflow/modules/ai_chat/',
  version: '481',
  isFolded: true,
  inputs: [
    Input_Template_SettingAiModel,
    // --- settings modal
    {
      key: NodeInputKeyEnum.aiChatTemperature,
      renderTypeList: [FlowNodeInputTypeEnum.hidden], // Set in the pop-up window
      label: '',
      value: 0,
      valueType: WorkflowIOValueTypeEnum.number,
    },
    {
      key: NodeInputKeyEnum.aiChatMaxToken,
      renderTypeList: [FlowNodeInputTypeEnum.hidden], // Set in the pop-up window
      label: '',
      value: 2000,
      valueType: WorkflowIOValueTypeEnum.number,
    },
    {
      key: NodeInputKeyEnum.aiChatEnableReasoning,
      renderTypeList: [FlowNodeInputTypeEnum.hidden],
      label: '',
      value: false,
      valueType: WorkflowIOValueTypeEnum.boolean,
    },
    {
      key: NodeInputKeyEnum.aiChatIsResponseText,
      renderTypeList: [FlowNodeInputTypeEnum.hidden],
      label: '',
      value: true,
      valueType: WorkflowIOValueTypeEnum.boolean,
    },
    AiChatQuoteRole,
    AiChatQuoteTemplate,
    AiChatQuotePrompt,
    {
      key: NodeInputKeyEnum.aiChatVision,
      renderTypeList: [FlowNodeInputTypeEnum.hidden],
      label: '',
      valueType: WorkflowIOValueTypeEnum.boolean,
      value: false,
    },
    // settings modal ---
    {
      ...Input_Template_System_Prompt,
      label: t('core.ai.Prompt'),
      description: systemPromptTip,
      placeholder: chatNodeSystemPromptTip,
    },
    Input_Template_History,
    Input_Template_Dataset_Quote,
    Input_Template_Text_Quote,

    { ...User_Input_Template_UserChatInput, toolDescription: t('user_question') },
  ],
  outputs: [
    {
      id: NodeOutputKeyEnum.history,
      key: NodeOutputKeyEnum.history,
      required: true,
      label: t('core.module.output.label.New context'),
      description: t('core.module.output.description.New context'),
      valueType: WorkflowIOValueTypeEnum.chatHistory,
      valueDesc: chatHistoryValueDesc,
      type: FlowNodeOutputTypeEnum.static,
    },
    {
      id: NodeOutputKeyEnum.answerText,
      key: NodeOutputKeyEnum.answerText,
      required: true,
      label: t('core.module.output.label.Ai response content'),
      description: t('core.module.output.description.Ai response content'),
      valueType: WorkflowIOValueTypeEnum.string,
      type: FlowNodeOutputTypeEnum.static,
    },
  ],
};
